package com.swordget.controller;

import com.swordget.commons.JdbcUtil;
import com.swordget.entity.DataSource;
import com.swordget.entity.vo.*;
import com.swordget.service.DataStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
import java.sql.DriverManager;

/**
 * @ClassName: DataStorageController
 * @Description: 数据仓库管理控制器
 * @Author: 陈根
 * @Date: 2020/7/2 15:54
 * @Version
 **/
@RestController
@RequestMapping("/storageManager")
public class DataStorageController {

     @Autowired
     private DataStorageService dataStorageService;

    /**
     * 数据源添加
     * @return
     */
    @PostMapping
    public SimpleResponseVo addSource(@RequestBody SimpleRequestVo<DataSource> dataSource){
        DataSource data = dataSource.getData();
        String userName = data.getUserName();
        String passWord = data.getPassWord();
        String ipAddress = data.getIpAddress();
        String dataType = data.getDataType();
        Boolean aBoolean = dataSourceTest(userName, passWord, ipAddress, dataType);
        if (!aBoolean){
            return SimpleResponseVo.fail("000001","保存数据源失败，ip地址，用户名或者密码错误！");
        }
        boolean add = dataStorageService.addDataSource(data);
        if (add){
            return SimpleResponseVo.success();
        }
        return SimpleResponseVo.fail("处理失败");
    }


    /**
     * 删除数据源方法
     * @param
     * @return
     */
    @DeleteMapping
    public SimpleResponseVo delDataSource(@RequestBody  SimpleRequestVo<DataSource> requestVo){
        DataSource data = requestVo.getData();
        String dataId = data.getSerialNo();

        boolean f=dataStorageService.delDataStorage(dataId);
        if (f){
            return SimpleResponseVo.success();
        }
        return SimpleResponseVo.fail("处理失败");
    }

    /**
     * 数据源表单展示
     * @param pageVo
     * @return
     */
    @GetMapping
    public SimplePageResponseVo getSource(SimplePageRequestVo pageVo){
        return dataStorageService.getDataSource(pageVo);

    }


    @PutMapping
    public SimpleResponseVo putSource(@RequestBody SimpleRequestVo<DataSource> dataSource){
        DataSource data = dataSource.getData();
        String userName = data.getUserName();
        String passWord = data.getPassWord();
        String ipAddress = data.getIpAddress();
        String dataType = data.getDataType();
        Boolean aBoolean = dataSourceTest(userName, passWord, ipAddress, dataType);
        if (!aBoolean){
            return SimpleResponseVo.fail("000001","修改数据源失败，ip地址，用户名或者密码错误！");
        }
      boolean  edit= dataStorageService.editDataSource(data);
        if (edit){
            return SimpleResponseVo.success();
        }
        return SimpleResponseVo.fail("处理失败");
    }
    //测试数据源能不能连接的方法
    public Boolean dataSourceTest(String userName,String passWord,String ipAddress,String dataType){
        if (dataType.equals("mysql")){
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                Connection connection = DriverManager.getConnection("jdbc:mysql://" + ipAddress + ":3306/commomimport?serverTimezone=UTC&useSSL=false&characterEncoding=utf-8"
                        , userName, passWord);
                 return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        if (dataType.equals("oracle")){
            try {
                Class.forName("oracle.jdbc.OracleDriver");
                Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@" + ipAddress + ":1521:ORACLE"
                        , userName, passWord);
               return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        if (dataType.equals("sqlServer")){
            try {
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                Connection connection = DriverManager.getConnection("jdbc:sqlserver://" + ipAddress + ":1433;DatabaseName=test"
                        , userName, passWord);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }

        return null;
    }

}
